package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.SecurityCode;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class SecurityCodeDao extends BaseDao<SecurityCode> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<SecurityCode> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<SecurityCode> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<SecurityCode> list = run.query(sql, new ResultSetHandler<List<SecurityCode>>() {
            @Override
            public List<SecurityCode> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    SecurityCode securityCode = new SecurityCode();

                    securityCode.setId(rs.getInt("id"));
                    securityCode.setCode(rs.getString("code"));
                    securityCode.setCreated(rs.getTimestamp("created"));
                    securityCode.setExpired(rs.getTimestamp("expired"));
                    securityCode.setKey(rs.getString("key_"));
                    securityCode.setStatus(rs.getInt("status"));
                    securityCode.setTarget(rs.getString("target"));
                    securityCode.setType(rs.getInt("type"));

                    list.add(securityCode);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  SecurityCode 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public SecurityCode selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, code, created, expired, key_, status, target, type from mto_security_code where id = ?";
        SecurityCode securityCode = run.query(sql,new ResultSetHandler<SecurityCode>() {
            @Override
            public SecurityCode handle(ResultSet rs) throws SQLException {
                SecurityCode securityCode = null;
                if(rs.next()) {
                    securityCode = new SecurityCode();

                        securityCode.setId(rs.getInt("id"));
                        securityCode.setCode(rs.getString("code"));
                        securityCode.setCreated(rs.getTimestamp("created"));
                        securityCode.setExpired(rs.getTimestamp("expired"));
                        securityCode.setKey(rs.getString("key_"));
                        securityCode.setStatus(rs.getInt("status"));
                        securityCode.setTarget(rs.getString("target"));
                        securityCode.setType(rs.getInt("type"));
                }
                return  securityCode;
            }
        },params);
        return securityCode;
    }

    /**
    *
    * @param securityCode 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(SecurityCode securityCode) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_security_code (  ");
        sql.append("  code, created, expired, key_, status, target, type ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            securityCode.getCode(),  
            securityCode.getCreated(),  
            securityCode.getExpired(),  
            securityCode.getKey(),  
            securityCode.getStatus(),  
            securityCode.getTarget(),  
            securityCode.getType()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  securityCode
    * @return
    * @throws SQLException
    */
    @Override
    public int update(SecurityCode securityCode) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_security_code           ");
        sql.append(" SET                    ");
            
            sql.append("   code  = ?,  ");
            sql.append("   created  = ?,  ");
            sql.append("   expired  = ?,  ");
            sql.append("   key_  = ?,  ");
            sql.append("   status  = ?,  ");
            sql.append("   target  = ?,  ");
            sql.append("   type  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                securityCode.getCode(), 
                securityCode.getCreated(), 
                securityCode.getExpired(), 
                securityCode.getKey(), 
                securityCode.getStatus(), 
                securityCode.getTarget(), 
                securityCode.getType(), 
            securityCode.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_security_code where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
